﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;

using Optimization.Utilities;

namespace Optimization.StandardProblems
{
    [IsProblem]
    public class Rastrigin : IProblem
    {
        int dim = 2;

        public Rastrigin(int Dim)
        {
            this.dim = Dim;
        }

        public Rastrigin()
        {
            this.dim = 2;
        }

        static public double Evaluate(Vector v)
        {
            double result = 0;

            for (int i = 0; i < v.Length; i++)
            {
                double x = v[i];
                result = result + 10 + x * x - 10 * Math.Cos(2 * Math.PI * x);
            }

            return result;

        }

        [CategoryAttribute("Settings"), DescriptionAttribute("Dimension")]
        public int Dim
        {
            get { return dim; }
            set { this.dim = value; }
        }

        double IProblem.Evaluate(Vector v)
        {
            return Evaluate(v);
        }

        void IProblem.GetBounds(out Vector mins, out Vector maxs)
        {
            mins = -5.12 * Vector.One(dim);
            maxs = +5.12 * Vector.One(dim);
        }

        bool IProblem.IsFeasible(Vector v)
        {
            return true;
        }

        Vector IProblem.RandomSolution()
        {
            Vector result = Vector.Random(dim, -5.12f, 5.12f);
            return result;
        }
    }
}